require "engine.class"
require "engine.Dialog"
local Savefile = require "engine.Savefile"
local Map = require "engine.Map"

module(..., package.seeall, class.inherit(engine.Dialog))

function _M:init(actor)
	self.actor = actor
	engine.Dialog.init(self, "Death!", 500, 300)

	self:generateList()

	self.sel = 1
	self.scroll = 1
	self.max = math.floor((self.ih - 45) / self.font_h) - 1

	self:keyCommands(nil, {
		MOVE_UP = function() self.sel = util.boundWrap(self.sel - 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end,
		MOVE_DOWN = function() self.sel = util.boundWrap(self.sel + 1, 1, #self.list) self.scroll = util.scroll(self.sel, self.scroll, self.max) self.changed = true end,
		ACCEPT = function() self:use() end,
	})
	self:mouseZones{
		{ x=2, y=10 + self.font:lineSkip()*6, w=350, h=self.font_h*self.max, fct=function(button, x, y, xrel, yrel, tx, ty)
			self.changed = true
			self.sel = util.bound(self.scroll + math.floor(ty / self.font_h), 1, #self.list)
			if button == "left" then self:use()
			end
			self.changed = true
		end },
	}
end

--- Clean the actor from debuffs/buffs
function _M:cleanActor()
	local effs = {}

	-- Go through all spell effects
	for eff_id, p in pairs(self.actor.tmp) do

		local e = self.actor.tempeffect_def[eff_id]
		effs[#effs+1] = {"effect", eff_id}
	end

	-- Go through all sustained spells
	for tid, act in pairs(self.actor.sustain_talents) do
		if act then
			effs[#effs+1] = {"talent", tid}
		end
	end

	while #effs > 0 do
		local eff = rng.tableRemove(effs)

		if eff[1] == "effect" then
			self.actor:removeEffect(eff[2])
		else
			local old = self.actor.energy.value
			self.actor:useTalent(eff[2])
			-- Prevent using energy
			self.actor.energy.value = old
		end
	end
end

--- Restore ressources
function _M:restoreRessources()
	self.actor.life = self.actor.max_life
	self.actor.power = self.actor.max_power

	self.actor.energy.value = game.energy_to_act
end

--- Basic resurection
function _M:resurrectBasic()
	self.actor.dead = false
	self.actor.died = (self.actor.died or 0) + 1

	local x, y = util.findFreeGrid(self.actor.x, self.actor.y, 20, true, {[Map.ACTOR]=true})
	if not x then x, y = self.actor.x, self.actor.y end
	self.actor.x, self.actor.y = nil, nil

	self.actor:move(x, y, true)
	game.level:addEntity(self.actor)
	game:unregisterDialog(self)
	game.level.map:redisplay()
end

function _M:use()
	if not self.list[self.sel] then return end
	local act = self.list[self.sel].action

	if act == "exit" then
		local save = Savefile.new(game.save_name)
		save:delete()
		save:close()
		util.showMainMenu()
	elseif act == "cheat" then
		game.logPlayer(self.actor, "#LIGHT_BLUE#You resurrect! CHEATER !")

		self:cleanActor()
		self:restoreRessources()
		self:resurrectBasic()
	end
end

function _M:generateList()
	local list = {}

	if config.settings.tome.cheat then list[#list+1] = {name="Resurrect by cheating", action="cheat"} end

	list[#list+1] = {name="Character dump", action="dump"}
	list[#list+1] = {name="Exit to main menu", action="exit"}

	self.list = list
end

function _M:drawDialog(s)
	local help = ([[You have #LIGHT_RED#died#LAST#!
The tides of war sweep on past you, your life extinguished.  You will never know the victory or defeat for your master.
Up in his castle, your master cares not of your death, you were just a paltry pawn in his game of war.
]]):splitLines(self.iw - 10, self.font)

	local h = 2
	local r, g, b
	for i = 1, #help do
		r, g, b = s:drawColorString(self.font, help[i], 5, h, r, g, b)
		h = h + self.font:lineSkip()
	end
	h = h + self.font:lineSkip()

	self:drawWBorder(s, 2, h - 0.5 * self.font:lineSkip(), self.iw - 4)

	self:drawSelectionList(s, 2, h, self.font_h, self.list, self.sel, "name")
	self.changed = false
end
